package com.woniuxy.threadpool;

import lombok.extern.slf4j.Slf4j;

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.temporal.ChronoField;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.*;

import static com.woniuxy.untis.Sleep.sleep;

/**
 * <P> @Author: ideaAdmin
 * <P> TODO: Class
 * <P> TODO:如何让每周四18：00：00定时执行任务
 * <P> Date: 2022/4/10:19:50
 * <P> Description:
 */
@Slf4j(topic = "c.c")
public class TimerTest02 {
    private static final ScheduledExecutorService pool =
            Executors.newScheduledThreadPool(1);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //TODO 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);

        //TODO 修改到周四时间 18点0份0秒0毫秒
        LocalDateTime time = now.withHour(20).withMinute(35).withSecond(20).withNano(0).with(DayOfWeek.SUNDAY);

        //TODO 如果当前时间 > 本周时间 需要设置为下周时间
        if (now.compareTo(time) > 0) {
            time = time.plusWeeks(1);
        }
        System.out.println(time);

        //TODO 代表当前时间和周四的时间差
        long initialDelay = Duration.between(now, time).toMillis();

        //TODO 一周的间隔时间
        long period = 1000 * 60 * 60 * 24 * 7;
        pool.scheduleAtFixedRate(() -> {
            log.debug("提醒时间");
            //TODO 多久后开始执行 每隔多久开始重复执行 时间单位
        }, initialDelay, period, TimeUnit.MILLISECONDS);
    }


}
